package cn.nabaixin.sign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.nabaixin.R;
import cn.nabaixin.setting.SettingActivity;
import cn.nabaixin.utils.AppUtils;
/**
 * 签到主界面
 * @author ZWH
 *
 */
public class SignActivity extends ListActivity {
	private ListView lv;
	private SimpleAdapter sa;
	private List<Map<String, Object>> data;
	private TextView refreshView;
	private TextView refreshTimeView;
	private View headerView;
	private View footView;
	private TextView positionText;
	private Context c;
	
	//菜单项
	public final int MENU_REFRESH = 1;
	public final int MENU_FILTER = 2;
	public final int MENU_SEARCH = 3;
	public final int MENU_AR = 4;
	public final int MENU_SETTING = 5;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		c = SignActivity.this;
		initViews();
		initListener();
		initData();
		addPositionText();
	}

	private void initViews() {
		lv = getListView();
		lv.setBackgroundColor(Color.WHITE);
		lv.setCacheColorHint(0);
		headerView = getLayoutInflater().inflate(R.layout.latest_list_header, null);
		footView = getLayoutInflater().inflate(R.layout.sign_list_footer, null);
		lv.addHeaderView(headerView);
		lv.addFooterView(footView);
	}

	private void initListener() {
		View.OnClickListener clickListener = new ClickListener();
		refreshView = (TextView) headerView.findViewById(R.id.latest_list_refresh);
		refreshTimeView = (TextView) headerView.findViewById(R.id.latest_list_refershTime);
		refreshView.setOnClickListener(clickListener);
		refreshTimeView.setOnClickListener(clickListener);
		footView.setOnClickListener(clickListener);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position==0 || position==3)
					return;
				Intent intent = new Intent(c, SignInActivity.class);
				startActivity(intent);
			}
			
		});
	}

	private void initData() {

		Integer[] icons = new Integer[]{R.drawable.ic_poitype_1, R.drawable.ic_poitype_2, R.drawable.ic_poitype_3, R.drawable.ic_poitype_4, R.drawable.ic_poitype_5, 
				R.drawable.ic_poitype_6, R.drawable.ic_poitype_7, R.drawable.ic_poitype_8, R.drawable.ic_poitype_9, R.drawable.ic_poitype_10};
		String peoples[] = new String[]{"10人来过这里", "20人来过这里", "30人来过这里", "40人来过这里", "50人来过这里", "60人来过这里"};
		String adds[] = new String[]{"北京西直门", "上地城铁", "时代之光1号楼", "马连洼北路59号", "北京市海淀区东北旺", "北京北站"};
		String distances[] = new String[]{"距离600米", "距离100米", "距离200米", "距离300米", "距离400米", "距离500米"};
		String names[] = new String[]{"张三", "李四", "王五", "小张", "小李", "小王"};
		
		data = new ArrayList<Map<String,Object>>();
		for(int i=0; i<7; i++){
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("icon", AppUtils.getRandomItem(icons));
			item.put("name", AppUtils.getRandomItem(names));
			item.put("adds", AppUtils.getRandomItem(adds));
			item.put("distance", AppUtils.getRandomItem(distances));
			item.put("peoples", AppUtils.getRandomItem(peoples));
			data.add(item);
		}
		
		HashMap<String, Object> item1 = new HashMap<String, Object>();
		item1.put("title", "我常去");
		data.add(0, item1);
		HashMap<String, Object> item2 = new HashMap<String, Object>();
		item2.put("title", "附近");
		data.add(3, item2);
		
		sa = new SimpleAdapter(c, data, R.layout.sign_list_item, 
				new String[]{"icon", "name", "adds", "distance", "peoples"}, 
				new int[]{R.id.sign_placeIcon, R.id.sign_placeName, R.id.sign_placeAdds, R.id.sign_placeDistance, R.id.sign_signPeps}){
					@Override
					public View getView(int position, View convertView, ViewGroup parent) {
						Map<String, Object> item = data.get(position);
						String title = (String) data.get(position).get("title");
						if(title!=null && !"".equals(title)){
							TextView tv = new TextView(c);
							tv.setTextColor(Color.BLACK);
							tv.setText(" "+title);
							tv.setBackgroundResource(R.drawable.bg_sign_item);
							return tv;
						}else{
							View v = getLayoutInflater().inflate(R.layout.sign_list_item, parent, false);
							((ImageView)v.findViewById(R.id.sign_placeIcon)).setImageResource((Integer)item.get("icon"));
							((TextView)v.findViewById(R.id.sign_placeName)).setText((String)item.get("name"));
							((TextView)v.findViewById(R.id.sign_placeAdds)).setText((String)item.get("adds"));
							((TextView)v.findViewById(R.id.sign_placeDistance)).setText((String)item.get("distance"));
							((TextView)v.findViewById(R.id.sign_signPeps)).setText((String)item.get("peoples"));
							return v;
						}
						
					}
					
		};
		lv.setAdapter(sa);
	}
	/**
	 * 在底部添加一行当前用户所在位置的文本
	 */
	private void addPositionText(){
		View view =  (ViewGroup) lv.getParent();
		android.widget.FrameLayout.LayoutParams flp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		flp.gravity = Gravity.BOTTOM;
		positionText = new TextView(c);
		positionText.setTextColor(Color.BLACK);
		positionText.setText("西直门北大街附近(全部类型)");
		positionText.setBackgroundResource(R.drawable.bg_sign_position);
		((ViewGroup) view).addView(positionText, flp);
	}
	
	//添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_REFRESH, 0, "刷新").setIcon(R.drawable.menu_refresh);
		menu.add(0, MENU_FILTER, 1, "筛选").setIcon(R.drawable.menu_filter);
		menu.add(0, MENU_SEARCH, 2, "搜索").setIcon(R.drawable.menu_search);
		menu.add(0, MENU_AR, 3, "实景模式").setIcon(R.drawable.menu_ar);
		menu.add(0, MENU_SETTING, 4, "设置").setIcon(R.drawable.menu_setting);
		return super.onCreateOptionsMenu(menu);
	}
	//处理菜单点击事件
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
		case MENU_REFRESH:
			Toast.makeText(c, "MENU_REFRESH", 0).show();
			return true;
		case MENU_FILTER:
			Toast.makeText(c, "MENU_FILTER", 0).show();
			return true;
		case MENU_SEARCH:
			Toast.makeText(c, "MENU_SEARCH", 0).show();
			return true;
		case MENU_AR:
			Toast.makeText(c, "MENU_AR", 0).show();
			return true;
		case MENU_SETTING:
			Intent intent2 = new Intent(c, SettingActivity.class);
			startActivity(intent2);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private class ClickListener implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			switch (viewId) {
			case R.id.latest_list_refresh:
			case R.id.latest_list_refershTime:	//点击刷新
				Toast.makeText(c, "点击了刷新", 0).show();
				break;

			}
		}
		
	}
}
